/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author project
 */
public class StatusVerify extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Connection con = null;
        Statement st = null;
        RequestDispatcher rd = request.getRequestDispatcher("manage_jobseekers.jsp");
        String sql = "";
        try {
            con = Dbconn.getConn();
            st = con.createStatement();
            String str[] = request.getParameterValues("cmcheck[]");
            String username = "", jobid = "";
            for (int i = 0; i < str.length; i++) {
                if (i == 0) {
                    username = "'" + str[i] + "'";
                } else {
                    username += ",'" + str[i] + "'";
                }

            }
            jobid = request.getParameter("jobid");
            sql = "select email from student where username in(" + username + ")";
            String email = "";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                if (rs.getRow() == 1) {
                    email = rs.getString("email");
                } else {
                    email = email + "," + rs.getString("email");
                }

            }
            // out.println("username=" + username);

            if (request.getParameter("action") != null && request.getParameter("action").equals("sendmsg")) {
                sql = "select email from student where username in(" + username + ")";
                email = "";
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    if (rs.getRow() == 1) {
                        email = rs.getString("email");
                    } else {
                        email = email + "," + rs.getString("email");
                    }

                }
                EmailSender.sendMail(email, request.getParameter("sub"), request.getParameter("mssg"));
            } else if (request.getParameter("Disapprove") != null && request.getParameter("Disapprove").equals("Unverify HallTicket")) {
                sql = "update job_applied set hallticketstatus='Not Verified' where username in(" + username + ") and jobid='" + jobid + "'";
                st.executeUpdate(sql);

            } else if (request.getParameter("Verify") != null && request.getParameter("Verify").equals("Verify HallTicket")) {
                sql = "update job_applied set hallticketstatus='Verified' where username in(" + username + ") and jobid='" + jobid + "'";
                if (st.executeUpdate(sql) > 0) {

                    rs = st.executeQuery("select subject,content from emailmessage where id='5'");
                    if (rs.next()) {
                        String subject = rs.getString(1);
                        String content = rs.getString(2);
                        String jobtitle = "", compname = "";
                        Statement st1 = con.createStatement();
                        ResultSet rs1 = st1.executeQuery("select jobtitle,compname from jobs,company where jobs.compid=company.compid and jobid='" + jobid + "'");
                        if (rs1.next()) {
                            content=content.replace("%jobtitle%", rs1.getString(1));
                            content=content.replace("%compname%", rs1.getString(2));
                            //content=content.replace(content, content);
                        }

                        EmailSender.sendMail(email, subject, content);

                    }
                }
            }
            rd.forward(request, response);
        } catch (Exception e) {
            out.println(e);
            //rd.forward(request, response);
        } finally {
            out.close();
        }

    }
}
